package com.github.live.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.widget.TextView;


import com.github.live.R;
import com.github.livebase.bean.Channel;

import java.util.ArrayList;

/**
 * @author pj567
 * @date :2021/1/12
 * @description:
 */
public class LiveChannelItemAdapter extends SimpleRecyclerAdapter<Channel> {
    private int selectedChannelIndex = -1;
    private int activeChannelIndex = -1;
    private String format = "%d";
    @SuppressLint("ResourceType")
    public LiveChannelItemAdapter(String format) {
        super(R.layout.item_live_channel, new ArrayList<>());
        this.format = format;
    }

    public void setSelectedChannelIndex(int selectedChannelIndex) {
        if (selectedChannelIndex == this.selectedChannelIndex) return;
        int preSelectedChannelIndex = this.selectedChannelIndex;
        this.selectedChannelIndex = selectedChannelIndex;
        if (preSelectedChannelIndex != -1)
            notifyItemChanged(preSelectedChannelIndex);
        if (this.selectedChannelIndex != -1)
            notifyItemChanged(this.selectedChannelIndex);
        else if (this.activeChannelIndex != -1)
            notifyItemChanged(this.activeChannelIndex);
    }

    public void setActiveChannelIndex(int activeChannelIndex) {
        if (this.activeChannelIndex == activeChannelIndex) return;
        int preFocusedChannelIndex = this.activeChannelIndex;
        this.activeChannelIndex = activeChannelIndex;
        if (preFocusedChannelIndex != -1)
            notifyItemChanged(preFocusedChannelIndex);
        if (this.activeChannelIndex != -1)
            notifyItemChanged(this.activeChannelIndex);
    }

    public int getSelectedChannelIndex() {
        return selectedChannelIndex;
    }
    @Override
    void onBindViewHolder(SimpleViewHolder holder, Channel data, int position) {
        TextView tvChannelNum = holder.findViewById(R.id.tvChannelNum);
        TextView tvChannel = holder.findViewById(R.id.tvChannelName);
        tvChannelNum.setText(String.format(format, data.getNumber()));
        tvChannel.setText(data.getName());
        if (position != selectedChannelIndex && position == activeChannelIndex) {
            tvChannelNum.setTextColor(mContext.getResources().getColor(R.color.color_theme));
            tvChannel.setTextColor(mContext.getResources().getColor(R.color.color_theme));
        }
        else{
            tvChannelNum.setTextColor(Color.WHITE);
            tvChannel.setTextColor(Color.WHITE);
        }
    }

    public int getActiveChannelIndex() {
        return activeChannelIndex;
    }
}